<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Administrator extends CI_Controller {

    public function Administrator() {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->library('administrator_authenticate');
    }

    function test() {
        $this->load->view('test');
    }
    public function index() {
        $this->administrator_authenticate->check_login();
        $data['header'] = 'Welcome to Medal Management Administrator';
        $data['content'] = 'administrator';
        $data['jquery_ui'] = '';
        $data['jquery_dialog'] = '';
        $this->load->view('admin_template', $data);
    }

    public function logout() {
        return $this->administrator_authenticate->logout();
    }

    public function login() {
        if ($this->session->userdata('administrator_session') == true) {
            redirect(site_url('/administrator/'));
        }
        if (check_post('username') != '' && check_post('password') != '') {
            $username = get_post('username');
            $password = get_post('password');

            $username = htmlspecialchars($username, ENT_QUOTES);
            $password = htmlspecialchars($password, ENT_QUOTES);

            $result = $this->user_model->authenticate($username, $password);

            $login = $result[0];
            if ($login > 0) {
                //$id = $result[1];
                //$this -> session -> set_userdata('administrator_id', $id);
                $this->session->set_userdata('administrator_session', true);

                //$row = $this -> user_model -> get_user_permission(array('ID' => $id));
                redirect(site_url('/admin'));
            } else {
                $this->session->set_userdata('administrator_id', '');
                $this->session->set_userdata('administrator_session', false);
                $this->session->set_userdata('failed', 'ឈ្មោះអ្នកប្រើប្រាស់ ឬ ពាក្យសំងាត់របស់អ្នក​មិន​ត្រឹម​ត្រូវ​ទេ សូមព្យាយាមម្ដងទៀត');
                $this->load->view('login');
            }
        } else {
            $this->load->view('login');
        }
    }

}
